<template>
	<view>
		<!-- 自定义搜索组件 -->
<mySearch @click="gotoHandelSearch"></mySearch>
		<view class="scoll-view-container">
			 <scroll-view  class="left-scoll-view"  scroll-y="true" :style="{height:wh+'px'}">
			 	<block v-for="(item,i) in cateList" :key="i">
					<view :class="['left-scoll-view-item',i==active?'active':'']" @click="activeChange(i)">
						{{item.cat_name}}
					</view>
				</block>
			 </scroll-view>
			 <scroll-view  class="right-scoll-view" scroll-y="true"  :style="{height:wh+'px'}" :scroll-top="scollTop">
		       <view class="cate-lv2" v-for="(item2,i2) in cateLeve2" :key="i2">
		       	   <view class="cate-lv2-title">
		       	   	/ {{item2.cat_name}} /
		       	   </view>
				   <!-- 动态渲染三级分类的列表数据 -->
				   <view class="cate-lv3-list">
				   	<view class="cate-lv3-item" v-for="(item3,i3) in item2.children" :key="i3" @click="gotoGoodsList(item3)">
				   		 <image :src="item3.cat_icon" mode=""></image>
						 <text>{{item3.cat_name}}</text>
				   	</view>
				   </view>
		       </view>
			 </scroll-view>
		</view>
		
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
				mixins:[badgeMix],
		data() {
			return {
				//设备可用的高度
				wh:0,
				//当前索引项
				active:0,
				//获取分类列表的数据
				cateList:[],
				// 二级分类列表
				cateLeve2:[],
				// 滚动条距离顶部的距离
				scollTop:0
			};
		},
		onLoad() {
			this.getCateList()
		const sysInfo=uni.getWindowInfo()
		console.log(sysInfo)
		this.wh = sysInfo.windowHeight - 50
		},
		methods:{
			//获取分类列表数据的方法
			async getCateList() {
				//发请求
				const {data:res} = await uni.$http.get('/api/public/v1/categories')
				console.log(res)
				if(res.meta.status !== 200){
					return uni.$showMsg()
				}
				//请求成功后，为data数据赋值
				this.cateList = res.message
				// 为二级分类赋值
				this.cateLeve2 = res.message[0].children
			},
			activeChange(i){
				this.active = i
				this.cateLeve2 = this.cateList[i].children
				// 切换时，让顶部距离为0
				this.scollTop = this.scollTop? 0:1
			},
			// 点击三级分类跳转对应页面
			gotoGoodsList(item3){
				uni.navigateTo({
					url:'/subpkg/goods_lists/goods_lists?cid='+item3.cat_id
				})
			},
			gotoHandelSearch(){
				uni.navigateTo({
					url:'/subpkg/searchs/searchs'
				})
			}
		}
	}
</script>

<style lang="scss">
.scoll-view-container{
	display: flex;
}
.left-scoll-view{
	width: 120px;
	.left-scoll-view-item{
		background-color: #f7f7f7;
		line-height: 60px;
		text-align: center;
		font-size: 12px;
		&.active{
			background-color: #ffffff;
			position: relative;
			&::before{
				content: "";
				display: block;
				width: 3px;
				height: 30px;
				background-color: #c00000;
				position: absolute;
                top: 50%;
				left: 0;
				transform: translateY(-50%);
			}
		}
	}
}
.cate-lv2-title{
	font-size: 12px;
	font-weight: blob;
	text-align: center;
	padding: 15px 0;
}
.cate-lv3-list{
	display: flex;
	flex-wrap: wrap;
	.cate-lv3-item{
		width: 33.33%;
		display: flex;
		flex-direction:column;
		justify-content: center;
		align-items: center;
		margin-bottom: 10px;
		image{
			width: 60px;
			height: 60px;
		}
		text{
			font-size: 12px;
		}
	}
}

</style>
